package Game 
{
	import com.greensock.OverwriteManager;
	import com.greensock.plugins.ScrollRectPlugin;
	import com.greensock.plugins.TweenPlugin;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import Common.Utils;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import Game.Windows.DebugWindow;
	/**
	 * ...
	 * @author Yevgeniy Logachev
	 */
	public class GameCamera 
	{
		private static const 	s_instance:GameCamera 	= new GameCamera();
		
		private static const	m_minScrollX:int		= 15;
		private static const	m_minScrollY:int		= 15;
		
		private var 			m_x:int 				= 0;
		private var 			m_y:int 				= 0;
		
		private var 			m_koefDurX:Number 		= 1.60 / Settings.screenHalfWidth;
		private var 			m_koefDurY:Number 		= 1.28 / Settings.screenHalfHeight;		
		
		public function GameCamera() 
		{
			if (s_instance == null)
			{
				trace ("GameCamera initialized");
			}
			else
			{
				trace("ERROR: Already existing instance in Singleton");
				throw new Error("Already existing instance in Singleton");
			}
		}
		
		public static function get instance():GameCamera
		{
			TweenPlugin.activate([ScrollRectPlugin]);
			OverwriteManager.init(OverwriteManager.AUTO);
			return s_instance;
		}		
		
		public function scroll(posx:int, posy:int):void
		{
			if ((Math.abs(posx - Settings.screenHalfWidth - m_x) < m_minScrollX) &&
				(Math.abs(posy - Settings.screenHalfHeight - m_y) < m_minScrollY))
			{
				return;
			}
			
			m_x = int(posx - Settings.screenHalfWidth);
			m_y = int(posy - Settings.screenHalfHeight);
			
			var duration:Number = (Math.abs.apply(null, [m_x]) > Math.abs.apply(null, [m_y])) ? 2.2 : 1.6;
			
			TweenMax.to(Main.mainSprite, duration, {scrollRect:{x:m_x, y:m_y, width:Settings.screenWidth, height:Settings.screenHeight}, overwrite:5});
		}
		
		public function goTo(posx:int, posy:int):void
		{
			Main.mainSprite.scrollRect = new Rectangle(posx - Settings.screenHalfWidth, posy - Settings.screenHalfHeight, Settings.screenWidth, Settings.screenHeight);
		}
		
		public function moveTo(dx:int, dy:int):void
		{
			Main.mainSprite.x += dx;
			Main.mainSprite.y += dy;
		}
	}

}
